<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Dao;

use App\Exception\CustomException;
use App\Model\User;
use App\Model\UserBill;

use Hyperf\DbConnection\Db;

/**
 * 用户 Dao
 *
 * @author 刘兴永(aile8880@qq.com)
 * @package App\Dao
 */
class UserDao extends Dao
{
    /**
     * @var string
     */
    const INTEGRAL = 'integral';

    /**
     * @var string
     */
    const MONEY = 'money';

    /**
     * 用户角色-会员
     *
     *@var int
     */
    const USER_MEMBER = 1;

    /**
     * 用户角色-渠道商
     *
     *@var int
     */
    const USER_CHANNEL_BUSINESS  = 2;

    /**
     * 用户角色-商家
     *
     *@var int
     */
    const USER_BUSINESS  = 3;

    /**
     * 通过ID获取用户
     *
     * @param int $id ID
     * @param bool $abnormal 是否抛异常
     * @return mixed
     */
    public function first(int $id, bool $abnormal = true)
    {
        $result = User::query()->where('id', $id)->first();

        if (!$result && $abnormal === true) {
            $this->error('USER.NOT_FOUND');
        }

        return $result;
    }

    /**
     * 通过手机号码登陆或创建用户
     *
     * @param string $phone 手机号码
     * @param string $login_pwd 登录密码
     * @param int $parent_id 上级ID
     * @return mixed
     */
    public function loginByPhoneOrCreate(string $phone, $login_pwd, int $parent_id)
    {
        return User::query()->firstOrCreate(['phone' => $phone], [
            'parent_id' => $parent_id,
            'name' => $phone,
            'login_pwd' => $login_pwd,
            'status' => true,
            'group_id' => 1
        ]);
    }

    /**
     * 通过手机号码登陆或创建用户
     *
     * @param string $phone 手机号码
     * @return mixed
     */
    public function loginByPhone(string $phone)
    {
        return User::query()->where(['phone' => $phone])->first();
    }

    /**
     * 修改数据
     *
     * @param array $data
     * @return int
     */
    public function update(array $data)
    {
        return User::query()->where('id', uid())->update($data);
    }

    /**
     * 通过邀请码获得上级
     *
     * @param string $identity_code 邀请码
     * @param bool $abnormal 是否抛异常
     * @return mixed
     */
    public function getParentByIdentityCode(string $identity_code, bool $abnormal = true)
    {
        $result = User::query()->where('identity_code', $identity_code)->select('id')->first();

        if (!$result && $abnormal === true) {
            $this->error('USER.INVITATION_CODE_NOT_FOUND');
        }

        return $result;
    }

    /**
     * 通过手机号查询数据
     * @param string $phone
     * @param array $select
     * @return mixed
     */
    public function getPhoneByUser( string $phone, array $select)
    {
        $result = User::query()->where('phone', $phone)->select($select)->first();

        if( !$result ) {
            $this->error('USER.PHONE_FAIL');
        }

        return $result;
    }

    /**
     * 找回密码
     *
     * @param array $data
     * @param array $where
     * @return int
     */
    public function retrieve( array $data, array $where)
    {
        return User::query()->where($where)->update($data);
    }

    /**
     * 自增
     *
     * @param int $user_id
     * @param int $num
     * @param string $column
     * @return int
     */
    public function increment(int $user_id, int $num, string $column)
    {
        return User::query()->where('id', $user_id)->increment($column, $num);
    }

    /**
     * 自减
     *
     * @param int $user_id
     * @param int $num
     * @param string $column
     * @return int
     */
    public function decrement(int $user_id, int $num, string $column)
    {
        return User::query()->where('id', $user_id)->decrement($column, $num);
    }

    /**
     * 统计会员分享数量
     *
     * @param array $map
     * @return mixed
     */
    public function count(array $map = [])
    {
        return User::query()
            ->where(function($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->count();
    }

    /**
     * 统计用户分享的会员为他带来积分收益的明细列表
     *
     * @param array $map
     * @param string $sort
     * @return mixed
     */
    public function getIntegralByMember(array $map = [] , $sort)
    {
        return User::query()
            ->with('userIntegral:id,user_id,num,order_no')
            ->where(function($query) use ($map) {
                return queryWhere($query, $map);
            })
            ->select('id','avatar','name','phone')
            ->orderByRaw($sort)
            ->paginate(10);
    }

    /**
     * 通过邀请码获取用户
     *
     * @param string $identity_code
     * @param string $field
     * @param bool $abnormal
     * @return mixed
     */
    public function getUserByIdentityCode(string $identity_code, $field = '*',$group,bool $abnormal = true )
    {
        $result = User::query()
            ->where('identity_code', $identity_code)
            ->where('group_id', $group)
            ->select($field)->first();

        if (!$result && $abnormal === true) {
            $this->error('USER.NOT_USER_CHANNEL_BUSINESS');
        }

        return $result;
    }
}